/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FuzzySolverMainFrame.java
 *
 * Created on 22.06.2010, 15:07:17
 */
package gui;

import tasks.FuzzyTask;
import java.awt.FileDialog;
import java.util.List;
import javax.swing.JComboBox;
import tasks.FuzzyRule;
import tasks.buildin.OelVerbrauch;

/**
 *
 * @author malygos
 */
public class FuzzySolverMainFrame extends javax.swing.JFrame {

    private FuzzyTask task;

    /** Creates new form FuzzySolverMainFrame */
    public FuzzySolverMainFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfFuzzyTask = new javax.swing.JTextField();
        btnFuzzyTaskBrowser = new javax.swing.JButton();
        btnSolveFuzzyTask = new javax.swing.JButton();
        canvas1 = new java.awt.Canvas();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("FuzzyTask");
        jLabel1.setEnabled(false);

        tfFuzzyTask.setEnabled(false);

        btnFuzzyTaskBrowser.setText("[...]");
        btnFuzzyTaskBrowser.setEnabled(false);
        btnFuzzyTaskBrowser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFuzzyTaskBrowserActionPerformed(evt);
            }
        });

        btnSolveFuzzyTask.setText("Solve");
        btnSolveFuzzyTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSolveFuzzyTaskActionPerformed(evt);
            }
        });

        label1.setText("UoD");

        jLabel2.setText("result");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfFuzzyTask, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnFuzzyTaskBrowser))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSolveFuzzyTask)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, 0, 322, Short.MAX_VALUE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label2, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(jLabel2))
                .addGap(64, 64, 64))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfFuzzyTask, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnFuzzyTaskBrowser))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSolveFuzzyTask)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFuzzyTaskBrowserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFuzzyTaskBrowserActionPerformed
        // TODO add your handling code here:
        FileDialog fd = new FileDialog(this, "Load", FileDialog.LOAD);
        fd.show();
        tfFuzzyTask.setText(fd.getDirectory() + fd.getFile());
        loadTask(tfFuzzyTask.getText());
    }//GEN-LAST:event_btnFuzzyTaskBrowserActionPerformed

    private void btnSolveFuzzyTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSolveFuzzyTaskActionPerformed
        OelVerbrauch verbrauch = new OelVerbrauch();
        float[] uod = verbrauch.getUoD(verbrauch.getLinguisticVariables().get(0));
        label2.setText("[" + uod[0] + ", " + uod[1] + "]");
        //verbrauch.drawGraphic(verbrauch.getLinguisticVariables().get(0), canvas1.getGraphics());
        //verbrauch.drawGraphicWithExactValue(verbrauch.getLinguisticVariables().get(0), canvas1.getGraphics(),17);
        //verbrauch.drawCompleteGraphic(verbrauch.getLinguisticVariables().get(0), canvas1.getGraphics(),17);

        float result = verbrauch.solve(canvas1.getGraphics(), new float[]{18.0f, 28.0f});
        jLabel3.setText("" + result);
        List<FuzzyRule> rules = verbrauch.getGeneratedRules();
        String[] s_rules = new String[rules.size()];
        for (int i = 0; i < rules.size(); i++) {
            s_rules[i] = rules.get(i).toString();
        }
        jComboBox1 = new JComboBox(s_rules);
    }//GEN-LAST:event_btnSolveFuzzyTaskActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FuzzySolverMainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFuzzyTaskBrowser;
    private javax.swing.JButton btnSolveFuzzyTask;
    private java.awt.Canvas canvas1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private javax.swing.JTextField tfFuzzyTask;
    // End of variables declaration//GEN-END:variables

    private void loadTask(String text) {
    }
}
